<script lang="ts" setup>
import type { LoginType } from '@/types/auth'
import { getCaptchaAPI } from '@/api/auth'
import { useUserStoreHook } from '@/stores/modules/user'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { initRouter } from '@/router/utils'
import { transformI18n } from '@/plugins/i18n'

// 获取主题色
const themeToken = getThemeTokenHook()
// 加载动画
const loading = ref(false)
const codeImg = ref<string | null>(null)
const userStore = useUserStoreHook()

const formState = reactive<LoginType>({
  username: 'admin',
  password: '123456',
  authType: 'password',
  code: '',
  codeKey: ''
})

const formRules = reactive({
  username: [{ required: true, message: transformI18n('login.check.username'), trigger: 'blur' }],
  password: [{ required: true, message: transformI18n('login.check.password'), trigger: 'blur' }],
  code: [{ required: true, message: transformI18n('login.check.code'), trigger: 'blur' }]
})

const router = useRouter()
const onFinish = (values: any) => {
  loading.value = true
  userStore
    .loginByPassword(formState)
    .then(() => {
      initRouter().then(() => {
        message.success(transformI18n('login.success'))
        router.replace('/admin')
      })
    })
    .finally(() => {
      loading.value = false
    })
}

/**
 * 获取验证码
 */
const getCaptcha = async () => {
  const res = await getCaptchaAPI()
  codeImg.value = res.data.img
  formState.codeKey = res.data.key
}

onMounted(() => {
  getCaptcha()
})
</script>
<template>
  <div class="login-form-container">
    <div
      :style="{
        background: themeToken.colorBgContainer,
        borderRadius: themeToken.borderRadiusLG + 'px'
      }"
      class="login-form"
    >
      <img alt="" class="logo" src="@/assets/logo.svg" />
      <div
        :style="{
          color: themeToken.colorTextBase
        }"
        class="logo-title"
      >
        SimpleAdmin
      </div>
      <a-form
        :model="formState"
        :rules="formRules"
        autocomplete="off"
        name="basic"
        @finish="onFinish"
      >
        <a-form-item :rules="formRules.username" name="username">
          <div class="form-item">
            <a-input
              v-model:value="formState.username"
              :placeholder="transformI18n('login.username')"
            >
              <template #prefix>
                <Icon type="icon-user" />
              </template>
            </a-input>
          </div>
        </a-form-item>
        <a-form-item :rules="formRules.password" name="password">
          <div class="form-item">
            <a-input-password
              v-model:value="formState.password"
              :placeholder="transformI18n('login.password')"
            >
              <template #prefix>
                <Icon type="icon-lock" />
              </template>
            </a-input-password>
          </div>
        </a-form-item>
        <a-form-item :rules="formRules.code" name="code">
          <div class="code form-item">
            <a-input v-model:value="formState.code" :placeholder="transformI18n('login.code')">
              <template #prefix>
                <Icon type="icon-safetycertificate" />
              </template>
            </a-input>
            <img
              :style="{
                borderRadius: themeToken.borderRadiusSM + 'px',
                border: '1px solid ' + themeToken.colorBorderSecondary,
                padding: '3px'
              }"
              @click="getCaptcha"
              alt=""
              class="code-img"
              :src="codeImg"
            />
          </div>
        </a-form-item>
        <div class="login-handle">
          <div class="left">
            <a-checkbox>{{ transformI18n('login.remember') }}</a-checkbox>
          </div>
          <div class="right">
            <a
              :style="{
                color: themeToken.colorPrimary
              }"
              >{{ transformI18n('login.forgotPassword') }}</a
            >
          </div>
        </div>
        <a-button
          class="login-submit"
          html-type="submit"
          size="large"
          :loading="loading"
          type="primary"
          >{{ transformI18n('login.submit') }}</a-button
        >
        <div class="login-other">
          <a-button type="default">{{ transformI18n('login.phone.submit') }}</a-button>
          <a-button type="default">{{ transformI18n('login.scanCode.submit') }}</a-button>
        </div>
      </a-form>
    </div>
  </div>
  <div
    :style="{
      background: themeToken.colorBgContainer
    }"
    class="login-bg"
  >
    <img alt="" class="login-bg-icon" src="@/assets/images/login_bg.svg" />
    <img alt="" class="login-bg-icon-bg" src="@/assets/images/login_icon_bg.png" />
  </div>
</template>

<style>
html {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}
</style>

<style scoped>
.login-bg {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: -1;
  .login-bg-icon {
    position: absolute;
    top: 50%;
    left: 10%;
    transform: translate(0, -50%);
    width: 350px;
  }
  .login-bg-icon-bg {
    height: 100vh;
  }
}
.login-form {
  width: 420px;
  top: 50%;
  transform: translate(0, -50%);
  right: 20%;
  position: fixed;
  padding: 10px 20px 30px 20px;
  animation: login-form-animation 0.5s ease-in-out;
  .login-handle {
    display: flex;
    padding: 5px 0;
    justify-content: space-between;
  }
  .logo {
    width: 100%;
    height: 80px;
    align-items: center;
    justify-content: center;
  }
  .logo-title {
    font-size: 25px;
    text-align: center;
    margin-top: 10px;
    margin-bottom: 20px;
  }
  .login-submit {
    width: 100%;
    margin-top: 20px;
  }
  .form-item {
    display: flex;
    height: 40px;
    width: 100%;
    text-align: center;
  }
  .code {
    display: flex;
    height: 40px;
    .code-img {
      cursor: pointer;
      margin-left: 20px;
      width: 100px;
      height: 100%;
    }
  }
  .login-other {
    display: grid;
    grid-template-columns: 1fr 1fr;
    margin-top: 20px;
    gap: 20px;
  }
}

@keyframes login-form-animation {
  0% {
    opacity: 0;
    right: 0;
  }
  50% {
    opacity: 0.8;
    right: 22%;
  }
  100% {
    opacity: 1;
    right: 20%;
  }
}
@media (max-width: 663px) {
  .login-bg {
    img {
      display: none;
    }
  }
  .login-form-container {
    padding: 30px;
    height: 100vh;
    display: flex;
    flex-direction: column;
  }
  .login-form {
    width: 100%;
    right: 0;
    bottom: 0;
    position: relative;
    box-shadow: v-bind(themeToken.boxShadowSecondary);
    animation: login-form-animation-m 0.5s ease-in-out;
  }
}
@keyframes login-form-animation-m {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
</style>
